<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



$urlid = (int) get('link');

if($DB->countRows($CONFIG->dbprefix.'jfxlinker_mapper', "id = '{$urlid}'")==0){
    JFX::addError($this->lang('invalid_id'));
    JFX::redirect($this->adminUrl);
}




// we have a valid url id












$uData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxlinker_mapper WHERE id = '{$urlid}'");





//error_reporting(E_ALL);
if(Antz_IntelliForm::submitted('edit-url')){
    $error = false;
    $url = post('url');
    $pageid = (int) post('page');
    $module = preg_replace('/[^a-zA-Z0-9]/', '', post('module'));
    $params = post('params');

    if(strlen(trim($params)) > 0 && substr($params, 0, 1)!=='?') $params = '?'.$params;

    $paramsArray = JFX::getUrlParamsAsArray($params);

    if(!is_array($paramsArray)) $paramsArray = array();

    if($module != 0 && !JFX_Module::isValidModule($module)){
        $error = true;
        JFX::addError($this->lang('invalid_module'));
    }

    if(substr($url, 0, 1)!=='/'){
        $error = true;
        JFX::addError($this->lang('invalid_url'));
    }

    if($url !== $uData['url']){
        $safeUrl = $DB->escape($url);
        if($DB->countRows($CONFIG->dbprefix.'jfxlinker_mapper', "url = '{$safeUrl}'")>0){
            JFX::addError($this->lang('url_already_used'));
            $error = true;
        }
    }
    
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$pageid}'")==0){
        $error = true;
        JFX::addError($this->lang('invalid_page'));
    }

    if(!$error){
        if($module==0) $module='';
        $this->mapUrl($url, $pageid, $module, $paramsArray);
        JFX::addSuccess($this->lang('edit_success'));
    }
}




























$uData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxlinker_mapper WHERE id = '{$urlid}'");

$paramParts = array();
$paramsTemp = unserialize($uData['params']);
if(!is_array($paramsTemp) || count($paramsTemp)==0){

}else{
    foreach($paramsTemp as $k=>$v){
        $paramParts[] = $k.'='.$v;
    }
}
$defaultParams = implode('&', $paramParts);

$moduleOpts = array('0'=>$this->lang('none'));
$pageOpts = array();

$modules = JFX_Module::getInstalledModules();

foreach($modules as $k=>$v){
    $moduleOpts[$v['keyname']] = $v['object']->getTitle();
}

$pagesRecursive = JFX::getPageTree(0, 1, true);

JFXLinker_Edit_AddPageOpts($pagesRecursive, $pageOpts, 0);


$formFields = array(
    array(
        'type' => 'text',
        'name' => 'url',
        'class' => 'wide',
        'label' => $this->lang('url'),
        'value' => post('url', $uData['url'])
    ),
    array(
        'type' => 'select',
        'name' => 'module',
        'label' => $this->lang('module'),
        'value' => post('module', $uData['module_key']),
        'options' => $moduleOpts
    ),
    array(
        'type' => 'select',
        'name' => 'page',
        'label' => 'page',
        'value' => post('page', $uData['page_id']),
        'options' => $pageOpts
    ),
    array(
        'type' => 'textarea',
        'name' => 'params',
        'value' => post('params', $defaultParams),
        'label' => $this->lang('params')
    ),
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'label' => '&nbsp;',
        'value' => $this->lang('submit')
    )
);

echo JFX::makeRapidForm($formFields, $this->lang('edit_legend'), 'edit-url');


























function JFXLinker_Edit_AddPageOpts($pages, &$opts, $depth=0){
    $LANG = JFX::registry('lang');
    foreach($pages as $k=>$v){
        $opts[$v['id']] = str_repeat('- ', $depth).$LANG->getContent('core', 'heading', $v['id']);
        if(count($v['subpages'])>0){
            JFXLinker_Edit_AddPageOpts($v['subpages'], $opts, $depth+1);
        }
    }
}